home *** CD-ROM | disk | FTP | other *** search
/ CD Actual 3 / CD ACTUAL 3.iso / linux / sonido / mod-0.000 / mod-0 / mod / sample.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-10-11  |  6.2 KB  |  252 lines

  1. /*
  2.  *  sample.c - Functions for handling samples.
  3.  *
  4.  *  (C) 1994 Mikael Nordqvist (d91mn@efd.lth.se, mech@df.lth.se)
  5.  */
  6.  
  7. #include <stdio.h>
  8. #include <unistd.h>
  9. #include <stdlib.h>
  10. #include <sys/types.h>
  11. #include <sys/ioctl.h>
  12. #include <sys/soundcard.h>
  13. #include <sys/ultrasound.h>
  14. #include <limits.h>
  15.  
  16. #include "mod.h"
  17.  
  18. /* External global variables */
  19.  
  20. SEQ_DECLAREBUF();
  21. extern int seqfd, gus_dev;
  22.  
  23. extern struct mod_info M;
  24. extern struct options opt;
  25.  
  26. int read_and_upload_sample(int fd, int i)
  27. {
  28.     int t, delta, base, skip, span, origlen;
  29.     struct patch_info *p;
  30.     struct sample_info *s;
  31.     char buf[128];
  32.  
  33.     skip=0; /* Not necessary, but removes a compiler warning */
  34.     
  35.     s=&M.sample[i];
  36.  
  37.     /* +2 for click-removal */
  38.     p=malloc(sizeof(struct patch_info)+s->length+2);
  39.  
  40.     p->key=GUS_PATCH;
  41.     p->device_no=gus_dev;
  42.     p->instr_no=i;
  43.     p->len=origlen=s->length;
  44.  
  45.     /* PAL = 8287 Hz ; NTSC = 8363 Hz */
  46.     if(s->c2freq == -1)
  47.     p->base_freq=(opt.ntsc_samples ? 8363 : 8287);
  48.     else
  49.     p->base_freq=s->c2freq;
  50.  
  51.     p->base_note=261632;            /* C-2 */
  52.     p->high_note=INT_MAX;
  53.     p->low_note=0;
  54.     p->panning=0;
  55.     p->detuning=0;
  56.     p->volume=64;      /* Not used in kernel when we have LINEAR volumes */
  57.     p->mode=0;         /* Default to signed 8 bit nonlooped samples      */
  58.  
  59.     /* Read the first 4 bytes and try to determine if we should skip the first
  60.      * 0, 2 or 4 bytes (amiga modules have a couple of 0-bytes at the start
  61.      * of samples to handle looping; 4 on older modules and 2 on newer ones).
  62.      * We never skip zeroes that are included in loops though (this is a
  63.      * special protection for short samples that often are looped and
  64.      * sound very strange if the looptime is shortened).
  65.      *
  66.      * The above scheme will cause samples to be a few bytes short (and thus
  67.      * played "a bit too early") but they will click less when triggered.
  68.      * If the sample is 16bit it can't be an amiga-sample and nothing is done.
  69.      */
  70.  
  71.     if(!s->bits_16) {
  72.     if(read(fd, p->data, 4) == 4) {
  73.         skip=0;
  74.         if(!p->data[0] && !p->data[1]) {
  75.         skip=2;
  76.         if(!p->data[2] && !p->data[3])
  77.             skip=4;
  78.         }
  79.         
  80.         if(s->looped)
  81.         skip=MIN(skip, s->repeat_start);
  82.         
  83.         lseek(fd, skip-4, SEEK_CUR);
  84.         p->len-=skip;
  85.         s->length=p->len;
  86.         s->repeat_start=MAX(0, s->repeat_start)-skip;
  87.         s->repeat_end=MAX(0, s->repeat_end-skip);
  88.     }
  89.     else {
  90.         free(p);
  91.         return 0;  /* Sample too short */
  92.     }
  93. #if 0
  94.     /* Show the first bytes of the sample */
  95.     if(opt.verbose >= 4) {
  96.         printf("%-22s: ", s->name);
  97.         for(t=0; t < skip; ++t)
  98.         printf("%4d ", (int)p->data[t]);
  99.     }
  100. #endif
  101.     }
  102.     
  103.     if(s->bits_16)
  104.     p->mode|=WAVE_16_BITS;
  105.  
  106.     if(s->looped) {
  107.     p->loop_start=s->repeat_start;
  108.     p->loop_end=s->repeat_end;
  109.     p->mode|=WAVE_LOOPING;
  110.     }
  111.  
  112.     if(s->unsigned_data)
  113.     p->mode|=WAVE_UNSIGNED;
  114.  
  115.     if(s->looped == LOOP_BIDIR)
  116.     p->mode|=WAVE_BIDIR_LOOP;
  117.  
  118.     /* Some modules seems to be 4 or 8 bytes short. Probably a
  119.      * moduleripper that didn't do it's job too well.
  120.      * Quick'n dirty solution: allow the last sample to be a bit short.
  121.      */
  122.     if((t=read(fd, p->data, p->len)) != p->len) {
  123.     if(p->len-t <= 8 || opt.tolerant) {
  124.         info("(Sample %d was %d bytes short)\n", i, (int)p->len-t);
  125.         p->len=t;
  126.         s->length=t;
  127.         if(s->repeat_end >=t) {
  128.         if(!s->bits_16) {
  129.             s->repeat_end=t-1;
  130.             p->loop_end=t-1;
  131.         }
  132.         else {
  133.             s->repeat_end=t-2;
  134.             p->loop_end=t-2;
  135.         }
  136.         }
  137.     }
  138.     else {
  139.         free(p);
  140.         return 0; /* Sample more than 8 bytes short, abort */
  141.     }
  142.     }
  143.  
  144. #if 0
  145.     /* Show the rest of the 10 first bytes of the sample */
  146.     if(!s->bits_16 && opt.verbose >= 4) {
  147.     for(t=0; t < 10-skip; ++t)
  148.         printf("%4d ", (int)p->data[t]);
  149.     printf("(%d)\n", skip);
  150.     }
  151. #endif
  152.     
  153. #if 0
  154.     /* Modify the sample after loop_end so the GF1 on the GUS interpolates
  155.      * correctly.
  156.      * This really doesn't work work if the last sample is included in the
  157.      * loop (has to be done by the kernel then).
  158.      */
  159.     
  160.     if(s->looped) {
  161.     if(!s->bits_16)
  162.         ((char *)p->data)[p->loop_end+1]=
  163.         ((char *)p->data)[p->loop_start];
  164.     else
  165.         ((short *)p->data)[p->loop_end/2+1]=
  166.         ((short *)p->data)[p->loop_start/2];
  167.     }
  168. #endif
  169.  
  170.     /* Attempt to minimize clicking/popping on looped samples by
  171.      * connecting samples at the loopingpoint with a straight line.
  172.      * Samples outside the loop will never be modified.
  173.      */
  174.     if(s->looped && opt.click_removal > 0 && origlen > opt.click_removal) {
  175.     span=2+1; /* Change 2 last bytes */
  176.     if(!s->bits_16) {
  177.         if(p->loop_end+1-span < p->loop_start)
  178.         span=p->loop_end-p->loop_start+1;
  179.         base=p->data[p->loop_end+1-span];
  180.         delta=p->data[p->loop_end+1]-base;
  181.         for(t=1; t<span; ++t)
  182.         p->data[p->loop_end+1-span+t]=(char)(base+(delta*t)/span);
  183.     }
  184.     else {
  185.         if(p->loop_end+(1-span)*2 < p->loop_start)
  186.         span=(p->loop_end-p->loop_start)/2+1;
  187.         base=((short *)p->data)[p->loop_end/2+1-span];
  188.         delta=((short *)p->data)[p->loop_end/2+1]-base;
  189.         for(t=1; t<span; ++t)
  190.         ((short *)p->data)[p->loop_end/2+1-span+t]=
  191.             (short)(base+(delta*t)/span);
  192.     }
  193.     }
  194.  
  195.     t=gus_dev;
  196.     ioctl(seqfd, SNDCTL_SYNTH_MEMAVL, &t);
  197.     if(p->len <= t)
  198.     SEQ_WRPATCH(p, sizeof(struct patch_info)+p->len);
  199.     
  200.     if(p->len > t) {
  201.     s->valid=0;
  202.     sprintf(buf, "Skipping sample %d", i);
  203.     print_status(buf);
  204.     info("Sample %d doesn't fit in GUS-memory - skipped.\n", i);
  205.     sleep(1);
  206.     }
  207.     else
  208.     s->valid=1;
  209.     
  210.     free(p);
  211.     
  212.     return 1;
  213. }
  214.  
  215.  
  216. void print_sample_info_header(void)
  217. {
  218.     if(!opt.verbose)
  219.     return;
  220.  
  221.     printf(
  222. "\n   #  Samplename               Size Finetune Loopstart Loopend Volume  #\n"
  223. "--------------------------------------------------------------------------\n");
  224. }
  225.  
  226.  
  227. void print_sample_info(int i)
  228. {
  229.     struct sample_info *s;
  230.  
  231.     if(!opt.verbose)
  232.     return;
  233.  
  234.     s=&M.sample[i];
  235.     if((opt.verbose && s->length) || (opt.verbose >= 2)) {
  236.     printf("%c %2d  %-22s %6d%c   %2d      %5d%c   %5d   %3d   %2d %c\n",
  237.            (s->length ? ' ' : '*'),
  238.            i,
  239.            (M.format == MODFORMAT_ULT ? s->dosname :
  240.         fill_string(s->name, 22)),
  241.            s->length,
  242.            (s->bits_16 ? '*' : ' '),
  243.            s->finetune,
  244.            s->repeat_start,
  245.            (s->looped == LOOP_BIDIR ? 'B' : ' '),
  246.            s->repeat_end,
  247.            s->volume,
  248.            i,
  249.            (s->length ? ' ' : '*'));
  250.     }
  251. }
  252.